package com.kaka.bus.engine.home.info.record;


import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kaka.bus.R;
import com.kaka.bus.adapter.RecordAdapter;
import com.kaka.bus.base.BaseActivity;
import com.kaka.bus.bean.HttpWrapper;
import com.kaka.bus.bean.MoneyRecordBean;
import com.kaka.bus.constant.Constant;
import com.kaka.bus.util.U;
import com.kaka.bus.view.MutipleLayout;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by zhangqi on 2016/11/7.
 */

public class RecordActivity extends BaseActivity<RecordPresenter> implements RecordContract.View {

    @BindView(R.id.toolbar_back)
    ImageView mToolbarBack;
    @BindView(R.id.toolbar_name)
    TextView mToolbarName;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.tv_record_type)
    TextView mTvRecordType;
    @BindView(R.id.tv_record_money)
    TextView mTvRecordMoney;
    @BindView(R.id.tv_record_time)
    TextView mTvRecordTime;
    @BindView(R.id.rl_record)
    LinearLayout mRlRecord;
    @BindView(R.id.record_recy)
    RecyclerView mRecordRecy;
    @BindView(R.id.record_smart)
    SmartRefreshLayout mRecordSmart;
    @BindView(R.id.record_muti)
    MutipleLayout mRecordMuti;

    private int mPage = 1;
    private int mPageSize = 20;

    private RecordAdapter mRecordAdapter;

    private List<MoneyRecordBean> mDatas = new ArrayList<>();

    @Override
    protected void initView() {
        super.initView();
        mToolbarBack.setOnClickListener(v -> finish());
        mToolbarName.setText("资金记录");

        mRecordRecy.setLayoutManager(new LinearLayoutManager(this));
        mRecordAdapter = new RecordAdapter(R.layout.item_record, mDatas);
        mRecordRecy.setAdapter(mRecordAdapter);
        mRecordAdapter.bindToRecyclerView(mRecordRecy);
        mRecordMuti.setEmptyText("您当前暂无资金记录!");
        mRecordMuti.setStatus(MutipleLayout.Loading);
        mRecordMuti.setEmptyImage(R.mipmap.empty_view);
        mRecordMuti.setEmptyBgColor(getResources().getColor(R.color.gray_bg));
        mRecordMuti.setErrorBgColor(getResources().getColor(R.color.gray_bg));
    }

    @Override
    protected void initData() {
        getData();
    }

    public void getData() {
        mDatas.clear();
        mPresenter.initData(getUserId(), getToken(), 1, mPageSize);
    }

    @Override
    public void onError() {
        mPage--;
        mRecordSmart.finishLoadmore(0);
        mRecordMuti.setStatus(MutipleLayout.Error);
    }

    @Override
    public void onResultStart(HttpWrapper<List<MoneyRecordBean>> data) {
        mRecordSmart.finishRefresh(0);
        if (Constant.SUCCESS_CODE.equals(data.code)) {
            if (data.data != null && data.data.size() > 0) {
                mRecordAdapter.addData(data.data);
                mRecordMuti.setStatus(MutipleLayout.Success);
            } else {
                mRecordMuti.setStatus(MutipleLayout.Empty);
            }
        } else if (Constant.ERROR_CODE.equals(data.getCode())) {
            showMessage(data.getInfo());
            onError();
        } else if (Constant.TOKEN_TIMEOUT.equals(data.code)) {
            U.tokenOut(this);
        }
    }

    @Override
    public void onResultLoadMore(HttpWrapper<List<MoneyRecordBean>> o) {
        mRecordSmart.finishLoadmore(0);
        if (Constant.SUCCESS_CODE.equals(o.getCode())) {
            if (Constant.SUCCESS_CODE.equals(o.getCode())) {
                if (o.data != null && o.data.size() > 0) {
                    mRecordAdapter.addData(o.data);
                } else {
                    showMessage("没有更多数据了");
                    mPage--;
                }
            }
        } else if (Constant.ERROR_CODE.equals(o.getCode())) {
            showMessage(o.getInfo());
            onError();
        } else if (Constant.TOKEN_TIMEOUT.equals(o.code)) {
            U.tokenOut(this);
        }
    }

    @Override
    protected void initListener() {
        mRecordSmart.setOnRefreshListener(refreshlayout -> {
            mPage = 1;
            getData();
        });

        mRecordSmart.setOnLoadmoreListener(refreshlayout -> {
            mPresenter.loadMore(getUserId(), getToken(), ++mPage, mPageSize);
        });

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_record;
    }


    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

}
